package banking;

import java.util.Date;

import com.mysql.jdbc.PreparedStatement;

/**
 * 交易记录类
 * @author 莫东林
 *
 */
public class Trade extends BaseOperation{

	int id;//交易编号
	String card_no;//甲方卡号
	String trade_no;//乙方卡号
	int type;//交易类型（0取款 1存款 2转账）
	Double balance;//交易金额
	Date time;//交易时间
	int status;//交易状态（0正常 1失败）
	
	/**
	 * 存、取款记录构造器
	 */
	public Trade(String card_no, int type, Double balance, int status){
		this.card_no = card_no;
		this.balance = balance;
		this.status = status;
		this.type = type;
		time = new Date();
	}
	
	/**
	 * 转账记录构造器
	 */
	public Trade(String card_no, String trade_no, Double balance, int status){
		this.card_no = card_no;
		this.trade_no = trade_no;
		this.balance = balance;
		this.status = status;
		type = 2;
		time = new Date();
	}
	
	/**
	 * 保存记录
	 * @throws Exception 
	 */
	@SuppressWarnings("deprecation")
	public boolean saveTrade(Trade trade){
		boolean temp = false;
		try {
			if(trade == null){
				return false;
			}else{
				switch(trade.type){
				//存取款
				case 0:
				case 1:
					sql = "insert into trade (card_no,type,balance,time,status) values(?,?,?,?,?)";
					stmt = (PreparedStatement) handler.getStmt(sql);
					break;
				//转账
				case 2:
					sql = "insert into trade (card_no,type,balance,time,status,trade_no) values(?,?,?,?,?,?)";
					stmt = (PreparedStatement) handler.getStmt(sql);
					stmt.setString(6, trade.trade_no);
					break;
				}
			}
			stmt.setString(1, trade.card_no);
			stmt.setInt(2, trade.type);
			stmt.setDouble(3, trade.balance);
			stmt.setString(4, trade.time.toLocaleString());
			stmt.setInt(5, trade.status);
			temp = stmt.execute();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return temp;
	}

	@Override
	public String toString() {
		return "Trade [id=" + id + ", card_no=" + card_no + ", trade_no=" + trade_no + ", type=" + type + ", balance="
				+ balance + ", time=" + time + ", status=" + status + "]";
	}
	
	
}
